home *** CD-ROM | disk | FTP | other *** search
- Path: acs.bu.edu!sseltser
- From: sseltser@bu.edu (Stan Seltser)
- Newsgroups: comp.lang.c++
- Subject: Global Objects Initialization
- Date: 20 Apr 1996 05:48:26 GMT
- Organization: Boston University
- Message-ID: <4l9tra$fcd@news.bu.edu>
- NNTP-Posting-Host: acs.bu.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Hello there,
-
- here is the question for you fans of C++:
- imagine class X and class Y
-
- class Y
- {
- int y;
- Y(int) {};
- ~Y() {};
- };
-
- class X {
-
- public:
-
- Y _y;
- static const Y Y1;
- static const Y Y2;
-
- X(Y y=Y1) { y=_y};
- ~X() {};
- }
-
- const Y X::Y1; // defined in some .cc file
- const Y X::Y2; // defined in some .cc file
-
- this all looks simple and compiles fine and even
- works without any problem
-
- as long as definitions of Y1 and Y2 are followed by
- actual declaration of class X everything is fine
-
- but imagine now that both declaration of Y1 and Y2 as
- well as class X happened in global scope which will cause
- problems since order of initialization is not guaranteed by
- compiler and so potentially upon entering X constrcutor
- Y1 may be not initalized
-
- so my question is:
-
- how can I guarantee that Y1 is always initialized before
- declaration of X type object?
-
- Scott Meyers in his wonderful book "effective C++ " has kinda
- solution with initializer class which in his constrcutor
- will initialize some data members and then objects which depend
- on this data members are initialized with example from iostream
- library , unfortunetely this is not the case
- const static cannot be initialized inside any constrcutor only
- globally...
-
- anybody has any ideas?
-
-